package com.ruoyi.workbench.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.workbench.domain.WorkMatter;
import com.ruoyi.workbench.domain.WorkTaskItem;
import com.ruoyi.workbench.mapper.WorkMatterMapper;
import com.ruoyi.workbench.mapper.WorkTaskItemMapper;
import com.ruoyi.workbench.service.IWorkTaskItemService;
import com.ruoyi.workbench.vo.TargetTaskItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.workbench.mapper.WorkTargetTaskMapper;
import com.ruoyi.workbench.domain.WorkTargetTask;
import com.ruoyi.workbench.service.IWorkTargetTaskService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 分解任务Service业务层处理
 * 
 * @author Guojl
 * @date 2024-07-04
 */
@Service
public class WorkTargetTaskServiceImpl implements IWorkTargetTaskService 
{
    @Autowired
    private WorkTargetTaskMapper workTargetTaskMapper;
    @Autowired
    private WorkTaskItemMapper workTaskItemMapper;
    @Autowired
    private WorkMatterMapper workMatterMapper;
    /**
     * 查询分解任务
     * 
     * @param targetTaskId 分解任务主键
     * @return 分解任务
     */
    @Override
    public WorkTargetTask selectWorkTargetTaskByTargetTaskId(Long targetTaskId)
    {
        WorkTargetTask workTargetTask = workTargetTaskMapper.selectWorkTargetTaskByTargetTaskId(targetTaskId);
        WorkTaskItem workTaskItem = new WorkTaskItem();
        workTaskItem.setWorkTargetTaskId(workTargetTask.getTargetTaskId());
        List<WorkTaskItem> workTaskItemList = workTaskItemMapper.selectWorkTaskItemList(workTaskItem);
        workTargetTask.setTaskItemList(workTaskItemList);
        return workTargetTask;
    }

    /**
     * 查询分解任务列表
     * 
     * @param workTargetTask 分解任务
     * @return 分解任务
     */
    @Override
    @DataScope(deptAlias = "t", userAlias = "t")
    public List<WorkTargetTask> selectWorkTargetTaskList(WorkTargetTask workTargetTask)
    {
        List<WorkTargetTask> workTargetTasks = workTargetTaskMapper.selectWorkTargetTaskList(workTargetTask);
        for (WorkTargetTask targetTask : workTargetTasks){
            WorkTaskItem workTaskItem = new WorkTaskItem();
            workTaskItem.setWorkTargetTaskId(targetTask.getTargetTaskId());
            List<WorkTaskItem> workTaskItemList = workTaskItemMapper.selectWorkTaskItemList(workTaskItem);
            targetTask.setTaskItemList(workTaskItemList);
            List<WorkMatter> taskMatterLIst = workMatterMapper.getAllByRelatedIndicatorOrTaskId(targetTask.getTargetTaskId().intValue());
            for (WorkMatter workMatter : taskMatterLIst) {
                targetTask.setTargetTaskStatus("4");
                if (!"4".equals(workMatter.getEm4())) {
                    targetTask.setTargetTaskStatus("3");
                    break;
                }
            }
        }
        return workTargetTasks;
    }

    /**
     * 查询分解任务列表
     *
     * @param workTargetTask 分解任务
     * @return 分解任务
     */
    @Override
    public TableDataInfo selectWorkTargetTaskForItemList(WorkTargetTask workTargetTask)
    {
        List<WorkTargetTask> workTargetTasks = workTargetTaskMapper.selectWorkTargetTaskList(workTargetTask);
        List<TargetTaskItem> targetTaskItemList = new ArrayList<>();
        for (WorkTargetTask targetTask : workTargetTasks){
            TargetTaskItem targetTaskItem = new TargetTaskItem();
            BeanUtils.copyProperties(targetTask, targetTaskItem);
            WorkTaskItem workTaskItem = new WorkTaskItem();
            workTaskItem.setWorkTargetTaskId(targetTask.getTargetTaskId());
            List<WorkTaskItem> workTaskItemList = workTaskItemMapper.selectWorkTaskItemList(workTaskItem);
            for (WorkTaskItem taskItem : workTaskItemList){
                targetTaskItem.setTaskItemId(taskItem.getTaskItemId());
                targetTaskItem.setTaskContent(taskItem.getTaskContent());
                targetTaskItemList.add(targetTaskItem);
            }
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(targetTaskItemList);
        rspData.setTotal(new PageInfo(workTargetTasks).getTotal());
        return rspData;
    }

    /**
     * 新增分解任务
     * 
     * @param workTargetTask 分解任务
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int insertWorkTargetTask(WorkTargetTask workTargetTask)
    {
        workTargetTask.setCreateTime(DateUtils.getNowDate());
        workTargetTask.setCreateBy(SecurityUtils.getUserId().toString());
        workTargetTask.setTargetTaskStatus("1");
        int insertRow = workTargetTaskMapper.insertWorkTargetTask(workTargetTask);
        List<WorkTaskItem> taskItemList = workTargetTask.getTaskItemList();
        for (WorkTaskItem taskItem : taskItemList){
            taskItem.setWorkTargetId(workTargetTask.getWorkTargetId());
            taskItem.setWorkTargetTaskId(workTargetTask.getTargetTaskId());
        }
        int itemRow = workTaskItemMapper.insertTaskItemList(taskItemList);
        return insertRow + itemRow;
    }

    /**
     * 新增分解任务
     *
     * @param workTargetTask 分解任务
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int insertWorkTargetTask(List<WorkTargetTask> workTargetTasks)
    {
        int row = 0;
        for (WorkTargetTask workTargetTask : workTargetTasks) {
            workTargetTask.setCreateTime(DateUtils.getNowDate());
            workTargetTask.setCreateBy(SecurityUtils.getUserId().toString());
            workTargetTask.setTargetTaskStatus("1");
            int insertRow = workTargetTaskMapper.insertWorkTargetTask(workTargetTask);
            List<WorkTaskItem> taskItemList = workTargetTask.getTaskItemList();
            for (WorkTaskItem taskItem : taskItemList){
                taskItem.setWorkTargetId(workTargetTask.getWorkTargetId());
                taskItem.setWorkTargetTaskId(workTargetTask.getTargetTaskId());
            }
            int itemRow = workTaskItemMapper.insertTaskItemList(taskItemList);
            row += (insertRow + itemRow);
        }
        return row;
    }

    /**
     * 修改分解任务
     * 
     * @param workTargetTask 分解任务
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int updateWorkTargetTask(WorkTargetTask workTargetTask)
    {
        List<WorkTaskItem> taskItemList = workTargetTask.getTaskItemList();
        List<WorkTaskItem> insertItemList = new ArrayList<>();
        List<WorkTaskItem> updateItemList = new ArrayList<>();
        for (WorkTaskItem taskItem : taskItemList){
            if (taskItem.getTaskItemId() == null) {
                taskItem.setWorkTargetId(workTargetTask.getWorkTargetId());
                taskItem.setWorkTargetTaskId(workTargetTask.getTargetTaskId());
                insertItemList.add(taskItem);
            } else {
                updateItemList.add(taskItem);
            }
        }
        if (updateItemList.size() > 0){
            workTaskItemMapper.updateTaskItemList(updateItemList);
        }
        if (insertItemList.size() > 0){
            workTaskItemMapper.insertTaskItemList(insertItemList);
        }
        workTargetTask.setUpdateTime(DateUtils.getNowDate());
        workTargetTask.setUpdateBy(SecurityUtils.getUserId().toString());
        return workTargetTaskMapper.updateWorkTargetTask(workTargetTask);
    }

    /**
     * 修改分解任务
     *
     * @param workTargetTask 分解任务
     * @return 结果
     */
    @Override
    public int updateWorkTargetTaskById(WorkTargetTask workTargetTask)
    {
        workTargetTask.setUpdateTime(DateUtils.getNowDate());
        workTargetTask.setUpdateBy(SecurityUtils.getUserId().toString());
        return workTargetTaskMapper.updateWorkTargetTask(workTargetTask);
    }

    /**
     * 批量删除分解任务
     * 
     * @param targetTaskIds 需要删除的分解任务主键
     * @return 结果
     */
    @Override
    public int deleteWorkTargetTaskByTargetTaskIds(Long[] targetTaskIds)
    {
        return workTargetTaskMapper.deleteWorkTargetTaskByTargetTaskIds(targetTaskIds);
    }

    /**
     * 删除分解任务信息
     * 
     * @param targetTaskId 分解任务主键
     * @return 结果
     */
    @Override
    public int deleteWorkTargetTaskByTargetTaskId(Long targetTaskId)
    {
        return workTargetTaskMapper.deleteWorkTargetTaskByTargetTaskId(targetTaskId);
    }
}
